package com.ww.seckill.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.ww.seckill.entity.SeckillMessage;
import com.ww.seckill.entity.SeckillOrder;
import com.ww.seckill.entity.User;
import com.ww.seckill.service.IGoodsService;
import com.ww.seckill.service.IOrderService;
import com.ww.seckill.vo.GoodsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

/**
 * @author ww
 */
@Service
@Slf4j
public class MqReceiver {

    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IOrderService orderService;

    /**
     * 进行下单
     *
     * @param message  订单消息
     */
    @RabbitListener(queues = "secKillQueue")
    public void receive(String message) {
        log.info("接收到订单消息:{}", message);

        SeckillMessage seckillMessage = JSON.parseObject(message, SeckillMessage.class);
        Long goodsId = seckillMessage.getGoodsId();
        User user = seckillMessage.getUser();

        // 判断库存
        GoodsVo goodsVo = goodsService.getGoodsVoById(goodsId);
        if (goodsVo.getStockCount() < 1) {
            return;
        }
        // 判断是否重复下单
        ValueOperations valueOperations = redisTemplate.opsForValue();
        SeckillOrder seckillOrder = (SeckillOrder) valueOperations.get("order:" + user.getId() + ":" + goodsId);
        if (seckillOrder != null) {
            return;
        }
        // 下单
        try {
            orderService.secKill(user, goodsVo);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return;
        }
    }


    @RabbitListener(queues = "queue")
    public void receive(Object msg) {
        log.info("接收消息：" + msg);
    }

    @RabbitListener(queues = "fanout_queue_01")
    public void receive01(Object msg) {
        log.info("广播QUEUE01接收消息：" + msg);
    }

    @RabbitListener(queues = "fanout_queue_02")
    public void receive02(Object msg) {
        log.info("广播QUEUE02接收消息：" + msg);
    }

    @RabbitListener(queues = "direct_queue_01")
    public void receive03(Object msg) {
        log.info("DIRECT QUEUE01接受消息:" + msg);
    }

    @RabbitListener(queues = "direct_queue_02")
    public void receive04(Object msg) {
        log.info("DIRECT QUEUE02接受消息:" + msg);
    }

    @RabbitListener(queues = "topic_queue_01")
    private void receive05(Object msg) {
        log.info("TOPIC QUEUE01接收消息：" + msg);
    }

    @RabbitListener(queues = "topic_queue_02")
    private void receive06(Object msg) {
        log.info("TOPIC QUEUE02接收消息：" + msg);
    }

    @RabbitListener(queues = "header_queue_01")
    public void receive07(Message message) {
        log.info("HEADER QUEUE01接收Message对象：" + message);
        log.info("QUEUE01接收消息：", new String(message.getBody()));
    }

    @RabbitListener(queues = "header_queue_02")
    public void receive08(Message message) {
        log.info("HEADER QUEUE02接收Message对象：" + message);
        log.info("QUEUE02接收消息：", new String(message.getBody()));
    }

}
